package com.example.nuc_ms_beta.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.nuc_ms_beta.R;
import com.example.nuc_ms_beta.app.MyApplication;
import com.example.nuc_ms_beta.bean.activity.User;
import com.example.nuc_ms_beta.util.SharePreferencesUtil;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by bnhc on 17-6-3.
 * <p>
 * <p>
 * Login Activity
 */

public class LoginActivity extends Activity implements View.OnClickListener {

    private Button activity_login_bt;

    private EditText activity_login_name, activity_login_pass;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acitivity_login);
        initView();
        initBmob();
    }

    private void initBmob() {
        Bmob.initialize(this, MyApplication.APPID);
    }

    private void initView() {
        activity_login_bt = (Button) findViewById(R.id.activity_login_bt);
        activity_login_bt.setOnClickListener(this);
        activity_login_name = (EditText) findViewById(R.id.activity_login_name);
        activity_login_pass = (EditText) findViewById(R.id.activity_login_pass);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_login_bt:
                verify();
                break;
        }
    }

    private void verify() {
        BmobQuery<User> query = new BmobQuery<>();
        query.addWhereEqualTo("mobilePhoneNumber", activity_login_name.getText().toString());
        query.findObjects(new FindListener<User>() {
            @Override
            public void done(List<User> list, BmobException e) {
                if (list.size() == 0) {
                    Toast.makeText(MyApplication.context, "手机号未注册", Toast.LENGTH_SHORT).show();
                } else {
                    String password = list.get(0).getPasswrod();
                    if (activity_login_pass.getText().toString().equals(password)) {
                        //Next Time will not Login
                        SharePreferencesUtil.saveData(MyApplication.context, "isLogin", false);
                        Toast.makeText(MyApplication.context, "登录成功", Toast.LENGTH_SHORT).show();
                        SharePreferencesUtil.saveData(MyApplication.context, "userName", list.get(0).getUsername());
                        SharePreferencesUtil.saveData(MyApplication.context, "userObjectId", list.get(0).getObjectId());
                        //Intent intent = new Intent(LoginActivity.this, FirstActivity.class);
                        //startActivity(intent);
                        setResult(1);
                        finish();
                    } else {
                        Toast.makeText(MyApplication.context, "密码错误", Toast.LENGTH_LONG).show();
                    }
                }

            }
        });

    }
}
